﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Maticsoft.DBUtility;

namespace ly.DAL
{
    /// <summary>
    /// 数据访问类:news
    /// </summary>
    public partial class news
    {
        public news()
        { }
        #region  BasicMethod



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(ly.Model.news model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into news(");
            strSql.Append("news_name,news_content,news_createtime,news_alertime,news_seetime,news_isshow,news_recommand,news_type,news_image,news_isimage,news_sort)");
            strSql.Append(" values (");
            strSql.Append("@news_name,@news_content,@news_createtime,@news_alertime,@news_seetime,@news_isshow,@news_recommand,@news_type,@news_image,@news_isimage,@news_sort)");
            SqlParameter[] parameters = {
					//new SqlParameter("@news_id", SqlDbType.Int,4),
					new SqlParameter("@news_name", SqlDbType.NChar,50),
					new SqlParameter("@news_content", SqlDbType.Text),
					new SqlParameter("@news_createtime", SqlDbType.SmallDateTime),
					new SqlParameter("@news_alertime", SqlDbType.SmallDateTime),
					new SqlParameter("@news_seetime", SqlDbType.Int,4),
					new SqlParameter("@news_isshow", SqlDbType.Int,4),
					new SqlParameter("@news_recommand", SqlDbType.Int,4),
					new SqlParameter("@news_type", SqlDbType.Int,4),
					new SqlParameter("@news_image", SqlDbType.NChar,50),
					new SqlParameter("@news_isimage", SqlDbType.Int,4),
					new SqlParameter("@news_sort", SqlDbType.Int,4)};
            //parameters[0].Value = model.news_id;
            parameters[0].Value = model.news_name;
            parameters[1].Value = model.news_content;
            parameters[2].Value = model.news_createtime;
            parameters[3].Value = model.news_alertime;
            parameters[4].Value = model.news_seetime;
            parameters[5].Value = model.news_isshow;
            parameters[6].Value = model.news_recommand;
            parameters[7].Value = model.news_type;
            parameters[8].Value = model.news_image;
            parameters[9].Value = model.news_isimage;
            parameters[10].Value = model.news_sort;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ly.Model.news model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update news set ");
            strSql.Append("news_name=@news_name,");
            strSql.Append("news_content=@news_content,");
            strSql.Append("news_createtime=@news_createtime,");
            strSql.Append("news_alertime=@news_alertime,");
            strSql.Append("news_seetime=@news_seetime,");
            strSql.Append("news_isshow=@news_isshow,");
            strSql.Append("news_recommand=@news_recommand,");
            strSql.Append("news_type=@news_type,");
            strSql.Append("news_image=@news_image,");
            strSql.Append("news_isimage=@news_isimage,");
            strSql.Append("news_sort=@news_sort");
            strSql.Append(" where ");
            strSql.Append("news_id=@news_id");
            SqlParameter[] parameters = {
					new SqlParameter("@news_id", SqlDbType.Int,4),
					new SqlParameter("@news_name", SqlDbType.NChar,50),
					new SqlParameter("@news_content", SqlDbType.Text),
					new SqlParameter("@news_createtime", SqlDbType.SmallDateTime),
					new SqlParameter("@news_alertime", SqlDbType.SmallDateTime),
					new SqlParameter("@news_seetime", SqlDbType.Int,4),
					new SqlParameter("@news_isshow", SqlDbType.Int,4),
					new SqlParameter("@news_recommand", SqlDbType.Int,4),
					new SqlParameter("@news_type", SqlDbType.Int,4),
					new SqlParameter("@news_image", SqlDbType.NChar,50),
					new SqlParameter("@news_isimage", SqlDbType.Int,4),
					new SqlParameter("@news_sort", SqlDbType.Int,4)};
            parameters[0].Value = model.news_id;
            parameters[1].Value = model.news_name;
            parameters[2].Value = model.news_content;
            parameters[3].Value = model.news_createtime;
            parameters[4].Value = model.news_alertime;
            parameters[5].Value = model.news_seetime;
            parameters[6].Value = model.news_isshow;
            parameters[7].Value = model.news_recommand;
            parameters[8].Value = model.news_type;
            parameters[9].Value = model.news_image;
            parameters[10].Value = model.news_isimage;
            parameters[11].Value = model.news_sort;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from news ");
            strSql.Append(" where news_id =@news_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@news_id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ly.Model.news GetModel(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 news_id,news_name,news_content,news_createtime,news_alertime,news_seetime,news_isshow,news_recommand,news_type,news_image,news_isimage,news_sort from news ");
            strSql.Append(" where news_id =@WorkID ");
            SqlParameter[] parameters = {
					new SqlParameter("@WorkID", SqlDbType.Int,4)
			};
            parameters[0].Value = id;
            ly.Model.news model = new ly.Model.news();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ly.Model.news DataRowToModel(DataRow row)
        {
            ly.Model.news model = new ly.Model.news();
            if (row != null)
            {
                if (row["news_id"] != null && row["news_id"].ToString() != "")
                {
                    model.news_id = int.Parse(row["news_id"].ToString());
                }
                if (row["news_name"] != null)
                {
                    model.news_name = row["news_name"].ToString();
                }
                if (row["news_content"] != null)
                {
                    model.news_content = row["news_content"].ToString();
                }
                if (row["news_createtime"] != null && row["news_createtime"].ToString() != "")
                {
                    model.news_createtime = DateTime.Parse(row["news_createtime"].ToString());
                }
                if (row["news_alertime"] != null && row["news_alertime"].ToString() != "")
                {
                    model.news_alertime = DateTime.Parse(row["news_alertime"].ToString());
                }
                if (row["news_seetime"] != null && row["news_seetime"].ToString() != "")
                {
                    model.news_seetime = int.Parse(row["news_seetime"].ToString());
                }
                if (row["news_isshow"] != null && row["news_isshow"].ToString() != "")
                {
                    model.news_isshow = int.Parse(row["news_isshow"].ToString());
                }
                if (row["news_recommand"] != null && row["news_recommand"].ToString() != "")
                {
                    model.news_recommand = int.Parse(row["news_recommand"].ToString());
                }
                if (row["news_type"] != null && row["news_type"].ToString() != "")
                {
                    model.news_type = int.Parse(row["news_type"].ToString());
                }
                if (row["news_image"] != null)
                {
                    model.news_image = row["news_image"].ToString();
                }
                if (row["news_isimage"] != null && row["news_isimage"].ToString() != "")
                {
                    model.news_isimage = int.Parse(row["news_isimage"].ToString());
                }
                if (row["news_sort"] != null && row["news_sort"].ToString() != "")
                {
                    model.news_sort = int.Parse(row["news_sort"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select news_id,news_name,news_content,news_createtime,news_alertime,news_seetime,news_isshow,news_recommand,news_type,news_image,news_isimage,news_sort ");
            strSql.Append(" FROM news ");
           /* if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }*/
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" news_id,news_name,news_content,news_createtime,news_alertime,news_seetime,news_isshow,news_recommand,news_type,news_image,news_isimage,news_sort ");
            strSql.Append(" FROM news ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM news ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ImgID desc");
            }
            strSql.Append(")AS Row, T.*  from news T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "news";
            parameters[1].Value = "ImgID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}
